﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.IoC;
using ProjectResistance.Shell.Controllers;
using OpenNETCF;
using System.Reflection;

namespace ProjectResistance.Shell.Views
{
    internal partial class NumericView : ViewBase
    {
        private ResistorController Controller { get; set; }
        private Dictionary<ToleranceValues, string> ToleranceLabels { get; set; }

        [InjectionConstructor]
        public NumericView([ServiceDependency(EnsureExists = true)] ResistorController controller)
        {
            InitializeComponent();

            // get a list of tolerance value text for faster lookups later
            ToleranceLabels = new Dictionary<ToleranceValues, string>();
            Type toleranceType = typeof(ToleranceValues);
            foreach(var t in Enum2.GetNames(toleranceType))
            {
                FieldInfo info = toleranceType.GetField(t);
                string label = ((ToleranceLabelAttribute)info.GetCustomAttributes(typeof(ToleranceLabelAttribute), false)[0]).Label;
                ToleranceLabels.Add((ToleranceValues)Enum2.Parse(toleranceType, t), label);
            }

            Controller = controller;
            Controller.ResistanceChanged += new EventHandler<GenericEventArgs<decimal>>(Controller_ResistanceChanged);
            Controller.ToleranceChanged += new EventHandler<GenericEventArgs<ToleranceValues>>(Controller_ToleranceChanged);
        }

        void Controller_ToleranceChanged(object sender, GenericEventArgs<ToleranceValues> e)
        {
            toleranceLabel.Text = string.Format("(+/- {0})", ToleranceLabels[e.Value]);
        }

        void Controller_ResistanceChanged(object sender, GenericEventArgs<decimal> e)
        {
            resistanceDisplay.Text = e.Value.ToString();
        }
    }
}
